package com.locohamster;

import java.util.ArrayList;
import java.util.Random;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
 
public class GameView extends SurfaceView {
       private Bitmap bmp;
       private SurfaceHolder holder;
       private GameLoopThread gameLoopThread;
       private int x = 0; 
       private int xSpeed = 1;
       ArrayList<CircleHolder>lista =new ArrayList<CircleHolder>();
	private Random r;
      
       public GameView (Context context, AttributeSet attrs, int defStyle)
       {
    	super(context, attrs, defStyle);
    	initialise();
       }
       public GameView (Context context, AttributeSet attrs)
       {
    	  
    	 super(context, attrs);
    	 initialise();
       }

      

       public GameView(Context context) {
             super(context);
             initialise();
             
       }
	private void initialise() {
		 r = new Random(); 
		gameLoopThread = new GameLoopThread(this);
		 holder = getHolder();
		 holder.addCallback(new SurfaceHolder.Callback() {
 
		        @Override
		        public void surfaceDestroyed(SurfaceHolder holder) {
		               boolean retry = true;
		               gameLoopThread.setRunning(false);
		               while (retry) {
		                      try {
		                            gameLoopThread.join();
		                            retry = false;
		                      } catch (InterruptedException e) {
		                      }
		               }
		        }
 
		        @Override
		        public void surfaceCreated(SurfaceHolder holder) {
		               gameLoopThread.setRunning(true);
		               gameLoopThread.start();
		        }
 
		        @Override
		        public void surfaceChanged(SurfaceHolder holder, int format,
		                      int width, int height) {
		        }
		 });
		 bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
	}
 
       @Override
       protected void onDraw(Canvas canvas) {
             if (x == getWidth() - bmp.getWidth()) {
                    xSpeed = -1;
             }
             if (x == 0) {
                    xSpeed = 1;
             }
             x = x + xSpeed;
             canvas.drawColor(Color.BLACK);
             canvas.drawBitmap(bmp, x , 10, null);
             synchronized (lista) {
            	 for (CircleHolder a :lista)
                 {
            		 if (a.getRadius()>0)
            		 {
                	 canvas.drawCircle(a.getX(), a.getY(), a.getRadius(), a.getP());
                	 a.setRadius(a.getRadius()-a.getRatio());
            		 }
                 }
				
			}
            
           
       }
       @Override
       public boolean onTouchEvent(MotionEvent event) {

       	if(event.getAction()==MotionEvent.ACTION_DOWN)
       	{
       		Paint p= new Paint();
       		
       		int c = Color.argb(r.nextInt(255),r.nextInt(255),r.nextInt(255), r.nextInt(255));
       		 p.setColor(c);
     p.setAntiAlias(true);
       		synchronized (lista) {
       			lista.add(new CircleHolder(event.getX(), event.getY(), 50+r.nextInt(100), p,1+r.nextInt(10)));
			}
       		
       		Log.v("Cosmo", "przycisk "+event.getX()+" "+event.getY());
       	}
       		
       	return super.onTouchEvent(event);
       }
}